package com.xh.core.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/*
 * @Author daiwei
 * @Description
 * @Date 2018/11/14 17:29
 **/
public class CookieUtils {

    public static String getCookie(HttpServletRequest request, String name, String defaultValue) {
        Cookie cookie = getCookieObject(request,name);
        return cookie != null ? cookie.getValue() : defaultValue;
    }

    public static String getCookie(HttpServletRequest request,String name) {
        return getCookie(request,name, (String)null);
    }

    public static Integer getCookieToInt(HttpServletRequest request,String name) {
        String result = getCookie(request,name);
        return result != null ? Integer.parseInt(result) : null;
    }

    public static Integer getCookieToInt(HttpServletRequest request,String name, Integer defaultValue) {
        String result = getCookie(request,name);
        return result != null ? Integer.parseInt(result) : defaultValue.intValue();
    }

    public static Long getCookieToLong(HttpServletRequest request,String name) {
        String result = getCookie(request,name);
        return result != null ? Long.parseLong(result) : null;
    }

    public static Long getCookieToLong(HttpServletRequest request,String name, Long defaultValue) {
        String result = getCookie(request,name);
        return result != null ? Long.parseLong(result) : defaultValue.longValue();
    }

    public static Cookie getCookieObject(HttpServletRequest request, String name) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            Cookie[] var3 = cookies;
            int var4 = cookies.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                Cookie cookie = var3[var5];
                if (cookie.getName().equals(name)) {
                    return cookie;
                }
            }
        }

        return null;
    }

    public static Cookie[] getCookieObjects(HttpServletRequest request) {
        Cookie[] result = request.getCookies();
        return result != null ? result : new Cookie[0];
    }

    public static void setCookie(HttpServletResponse response, String name, String value, int maxAgeInSeconds, boolean isHttpOnly) {
        doSetCookie(response,name, value, maxAgeInSeconds, (String)null, (String)null, isHttpOnly);
    }

    public static void setCookie(HttpServletResponse response,String name, String value, int maxAgeInSeconds) {
        doSetCookie(response,name, value, maxAgeInSeconds, (String)null, (String)null, (Boolean)null);
    }

    public static void setCookie(HttpServletResponse response, Cookie cookie) {
        response.addCookie(cookie);
    }

    public static void setCookie(HttpServletResponse response,String name, String value, int maxAgeInSeconds, String path, boolean isHttpOnly) {
        doSetCookie(response,name, value, maxAgeInSeconds, path, (String)null, isHttpOnly);
    }

    public static void setCookie(HttpServletResponse response,String name, String value, int maxAgeInSeconds, String path) {
        doSetCookie(response,name, value, maxAgeInSeconds, path, (String)null, (Boolean)null);
    }

    public static void setCookie(HttpServletResponse response,String name, String value, int maxAgeInSeconds, String path, String domain, boolean isHttpOnly) {
        doSetCookie(response,name, value, maxAgeInSeconds, path, domain, isHttpOnly);
    }

    public static void removeCookie(HttpServletResponse response,String name) {
        doSetCookie(response,name, (String)null, 0, (String)null, (String)null, (Boolean)null);
    }

    public static void removeCookie(HttpServletResponse response,String name, String path) {
        doSetCookie(response,name, (String)null, 0, path, (String)null, (Boolean)null);
    }

    public static void removeCookie(HttpServletResponse response,String name, String path, String domain) {
        doSetCookie(response,name, (String)null, 0, path, domain, (Boolean)null);
    }

    private static void doSetCookie(HttpServletResponse response,String name, String value, int maxAgeInSeconds, String path, String domain, Boolean isHttpOnly) {
        Cookie cookie = new Cookie(name, value);
        cookie.setMaxAge(maxAgeInSeconds);
        if (path == null) {
            path = "/";
        }

        cookie.setPath(path);
        if (domain != null) {
            cookie.setDomain(domain);
        }

        if (isHttpOnly != null) {
            cookie.setHttpOnly(isHttpOnly.booleanValue());
        }

        response.addCookie(cookie);
    }
}
